﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Kxx_Common.Net
{
    public class EmailHelper
    {
        /// <summary>
        /// QQ邮箱
        /// </summary>
        /// <param name="fromAddr">发件邮箱</param>
        /// <param name="psw">授权码</param>
        /// <param name="toAddr">收件邮箱</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">邮件主体</param>
        public static void SendQQEmail(string fromAddr, string psw, string toAddr, string subject, string body)
        {
            MailMessage myMail = new MailMessage();

            myMail.From = new MailAddress(fromAddr);
            myMail.To.Add(new MailAddress(toAddr));

            myMail.Subject = subject;
            myMail.SubjectEncoding = Encoding.UTF8;

            myMail.Body = body;
            myMail.BodyEncoding = Encoding.UTF8;
            myMail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.qq.com";
            smtp.Credentials = new NetworkCredential(fromAddr, psw);
            smtp.EnableSsl = true; // 启用SSL/TLS加密
            smtp.Port = 587; // 或 465，取决于QQ邮箱的要求

            smtp.Send(myMail);
        }
    }
}
